package com.huluobo.lc.kotlinim1807ak.ui.fragment

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.lc.kotlinim1807ak.R
import com.huluobo.lc.kotlinim1807ak.adapter.ContactListAdapter
import com.huluobo.lc.kotlinim1807ak.adapter.EMContactListenerAdapter
import com.huluobo.lc.kotlinim1807ak.base.BaseFragment
import com.huluobo.lc.kotlinim1807ak.contract.contract.ContactContact
import com.huluobo.lc.kotlinim1807ak.contract.contract.ContactPresenter
import com.huluobo.lc.kotlinim1807ak.ui.activity.AddFriendActivity
import com.huluobo.lc.kotlinim1807ak.widget.SlideBar
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * @author Lc
 * @description: 联系人页面
 * @date :2021/2/25 9:59
 */
class ContactFragment : BaseFragment(), ContactContact.View {

    val presenter = ContactPresenter(this)

    override fun getLayoutResId(): Int = R.layout.fragment_contacts

    override fun init() {
        super.init()
        headerTitle.text = getString(R.string.contact)
        add.visibility = View.VISIBLE

        add.setOnClickListener { context?.startActivity<AddFriendActivity>() }

        swipeRefreshLayout.apply {
            //对当前控件或者类的创建者模式,kotlin高级语法
            setColorSchemeColors(resources.getColor(R.color.qq_blue))
            isRefreshing = true
            setOnRefreshListener { presenter.loadContacts() }
        }

        recyclerView.apply {
            setHasFixedSize(true)
            //当Recyclerview长度发生变化时,内部会做优化处理
            layoutManager = LinearLayoutManager(context)
            adapter = ContactListAdapter(context, presenter.contactListItems)
        }

        //初始化需要加载联系人数据
        presenter.loadContacts()

        EMClient.getInstance().contactManager()
            .setContactListener(object : EMContactListenerAdapter() {
                override fun onContactDeleted(username: String?) {
                    //当联系人被删除时,重新获取联系人数据
                    presenter.loadContacts()
                }
            })

        slideBar.onSectionChangeListener = object : SlideBar.OnSectionChangeListener {
            override fun onSectionChange(firstLetter: String) {
                section.visibility = View.VISIBLE
                section.text = firstLetter
                //
                recyclerView.smoothScrollToPosition(getPosition(firstLetter))
            }

            override fun onSlideFinish() {
                section.visibility = View.GONE
            }

        }
    }

    private fun getPosition(firstLetter: String): Int =
        //binarySearch 是二分查找的api
        presenter.contactListItems.binarySearch { it -> it.firstLetter.minus(firstLetter[0]) }


    override fun onLoadContactsSuccess() {
        //加载成功时隐藏下拉刷新
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.isRefreshing = false
        }
        //刷新适配器
        if (recyclerView != null) {
            recyclerView.adapter?.notifyDataSetChanged()
        }
    }

    override fun onLoadContactsFailed() {
        //加载失败时隐藏下拉刷新
        swipeRefreshLayout.isRefreshing = false
        //弹toast
        context?.toast(R.string.load_contacts_failed)
    }
}